﻿@using KPIS.GERP.Models.ViewModel
@model IEnumerable<KPIS.GERP.Models.ICM.DeliveryNote>
@{
    //Layout = "~/Views/Shared/_EmptyLayout.cshtml";
    ViewBag.Title = "รายการตรวจรับ [DN]";
    ViewBag.Descr = "ข้อมูลรายการที่ตรวจรับ";
    var poInstallmentsOfPo = (((PurchaseOrderInstallment[])ViewBag.PoInstallmentsOfPo) ?? new PurchaseOrderInstallment[0]);

    var db = new KPIS.GERP.Models.GERPContext();
    var DnItemList = db.DeliveryNoteItems.ToList();
    db.Dispose();
    //int intPoId = ViewBag.PoId;

}
@using (Html.BeginForm("DeleteAll", "DeliveryNote", FormMethod.Post, new { id = "form" }))
{

    <div class="block-button-menu-above">

        @Html.ActionLink("เพิ่มรายการจากรายการที่ยังไม่ตรวจรับ", "indexNotDelivery", "DeliveryNote"
        , new
        {
            id = "cmdCreateSub",
            role = "button",
            @class = "btn btn-small btn-success button-standard-width"
            ,
            data_dialog_id = "aboutDialog",
            data_dialog_title = "เพิ่ม"
        })

        @Html.ButtonDeleteAll()
    </div>

    <div class="row-fluid">

        @Html.ValidationSummary(true, "", new { @class = "alert alert-error" })

        <div class="row-fluid">
            <div class="table-header span12">
                <div class="span10">
                    รายการใบสั่งที่ตรวจรับ
                </div>
                @Html.Partial("_ExportButton")
            </div>
        </div>


        <table id="table" class="table table-striped table-bordered table-hover" aria-describedby="table_info">
            <thead>
                <tr>
                    <th class="center"> <label><input type="checkbox" /><span class="lbl"></span></label></th>
                    <th class="center">เลขที่การตรวจรับ</th>
                    <th class="center">ชื่อรายการตรวจรับ</th>
                    <th class="center" style="width:100px">เลขที่ใบสั่งซื้อ</th>
                    <th>งวดที่</th>
                    @*<th class="hidden-480" style="width:90px">วันที่สั่งซื้อ</th>
                        <th style="width:80px">ประเภท</th>*@
                    <th class="hidden-480" style="width:150px">ผู้ขาย/รับจ้าง</th>
                    <th class="hidden-480" style="width:100px">จำนวนเงิน</th>
                    <th class="hidden-480" style="width:100px">
                        เลขที่ PR
                    </th>
                    <th class="hidden-480" style="width:100px">สถานะ</th>
                    <th>&nbsp;</th>
                    @*<th class="center" style="width:80px;"><!-- Empty head column for export buttons --></th>
                        <th class="hidden-480">ส่งขอเบิก</th>*@
                </tr>
            </thead>

            <tbody>
                @{
    var items = (Model ?? new ArraySegment<DeliveryNote>()).OrderByDescending(o => o.Code);
    int i = 0;
                }
                @foreach (var m in items)
                {
                    <tr>
                        <td class="center">
                            <span style="display: none;">@(i++)</span>
                            @if (m.RecordStatus != "A" && m.RecordStatus != "P")
                            {
                                <label>
                                    <input type="checkbox" name="deleteIds" value="@m.DnId" />
                                    <span class="lbl"></span>

                                </label>
                            }
                        </td>
                        <td>
                            <a href="@Url.Action("Details", "DeliveryNote", new { DnId = m.DnId, PoId = m.PoId, PoInstallId = m.PoInstallId, _ = m.Code + "-" + (m.Name ?? "").Trim().Replace(" ", "") })">
                                @Html.DisplayFor(model => m.Code)
                            </a>
                        </td>
                        <td>
                            <a href="@Url.Action("Details", "DeliveryNote", new { DnId = m.DnId, PoId = m.PoId, PoInstallId = m.PoInstallId, _ = m.Code + "-" + (m.Name ?? "").Trim().Replace(" ", "") })">
                                @Html.DisplayFor(model => m.Name)
                            </a>
                        </td>
                        <td>
                            <a href="@Url.Action("Details", "PurchaseOrder", new { Id = m.PoId, _ = m.PurchaseOrder.Code + "-" + (m.PurchaseOrder.Name ?? "").Trim().Replace(" ", "") })">
                                @Html.DisplayFor(model => m.PurchaseOrder.Code) <i class="icon-external-link"></i>
                            </a>
                        </td>
                        <td>
                            @Html.DisplayFor(model => m.PurchaseOrderInstallment.InstallNumber)
                            @{
                    var poInstall = poInstallmentsOfPo.FirstOrDefault(o => o.PoInstallId == m.PoInstallId);
                            }
                        </td>
                        <td>
                            @Html.DisplayFor(model => m.PurchaseOrder.Bidder.MasContact.NameTh)
                        </td>
                        <td>
                            @((DnItemList.Where(s => s.DnId == m.DnId).Sum(s => s.NetAmount) ?? 0).ToString("#,##0.00"))
                        </td>
                        <td>
                            @Html.DisplayFor(model => m.PurchaseOrder.Bidder.PurchaseRequest.Code)
                        </td>
                        <td>
                            @*@Html.DisplayFor(model => m.PurchaseOrder.RecordStatus)*@
                            @{
                    var service = new KPIS.GERP.WEB.GIMS.RequestParcelService.RequestParcelServiceClient();
                    string status = @m.RecordStatus;
                    string result = service.GetStatusName(status).ToString();
                    ViewBag.StatusName = result;

                    //var service2 = new KPIS.GERP.WEB.GIMS.DeliveryNoteService.DeliveryNoteServiceClient();
                    //// var service = new KPIS.GERP.WEB.GIMS.RequestParcelService.RequestParcelServiceClient();

                    //var Dn = service2.SelectDeliveryNote(new KPIS.GERP.Models.ICM.DeliveryNote(), null)
                    //    .Where(o => (o.RecordStatus == "U" || o.RecordStatus == "A") && o.RecordStatus != "D");

                    ////string status = @m.PurchaseOrder.RecordStatus;
                    //var deliveryNote = Dn.FirstOrDefault(o => o.PoInstallId == m.PoInstallId);
                    //string status = deliveryNote != null ? deliveryNote.RecordStatus : null;

                    //string result;
                    //switch (status)
                    //{
                    //    case "U":
                    //        result = "ตรวจรับแล้ว";
                    //        break;
                    //    case "A":
                    //        result = "ตรวจรับแล้ว";
                    //        break;
                    //    case "W":
                    //        result = "ยังไม่ส่งอนุมัติ";
                    //        break;
                    //    case "P":
                    //        result = "รออนุมัติ";
                    //        break;
                    //    case "C":
                    //        result = "ยกเลิก";
                    //        break;
                    //    case "J":
                    //        result = "ไม่ผ่านการอนุมัติ";
                    //        break;
                    //    default:
                    //        result = "ยังไม่ตรวจรับ";
                    //        break;
                    //}
                    //ViewBag.StatusName = result;
                            }
                            @ViewBag.StatusName
                        </td>
                        <td>
                            @Html.ButtonDetails(new { DnId = m.DnId, PoId = m.PoId, PoInstallId = m.PoInstallId }, true)
                            @if (m.RecordStatus != "A" && m.RecordStatus != "P")
                            {
                                @Html.ButtonEdit(new { DnId = m.DnId }, true)@*, PoId=m.PoId, PoInstallId=m.PoInstallId*@
                                @Html.ButtonDelete(new { id = m.DnId }, true)@*, PoId=m.PoId, PoInstallId=m.PoInstallId*@
                            }
                        </td>
                    </tr>
                }


            </tbody>
        </table>
    </div>

}

<div id="modal" class="modal hide fade autosize" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="height:300px"></div>
@section scripts {



    <script type="text/javascript">


        $(function () {
            //var oTable1 = $('#table').dataTable({
            //"aoColumns": [
            //    { "bSortable": false },
            //    null,
            //    null,
            //    null,
            //    null,
            //    null,
            //    null,
            //    null,
            //    null,

            //    { "bSortable": false }
            //]
            //});
            var oTable1 = $('#table').dataTable({
                "aoColumns": [
                    { "bSortable": false },
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    { "bSortable": false }
                ]
            });

            $('table th input:checkbox').on('click', function () {
                var that = this;
                $(this).closest('table').find('tr > td:first-child input:checkbox')
				.each(function () {
				    this.checked = that.checked;
				    $(this).closest('tr').toggleClass('selected');
				});

            });

            var $modal = $('#modal');
            $('#cmdCreateSub').on('click', function (e) {
                /* user ไม่อยากให้เป็น Popup ขึ้นมา

                e.preventDefault();
			    var url = $(this).attr('href');
			    $modal.html('<iframe width="900" height="800"   frameborder="1" scrolling="auto" allowtransparency="true" src="' + url + '"></iframe>');
			    $modal.modal({
			        show: true
			    });*/
            });

            $modal.on('hide', function () {
                $modal.empty(); // Clean up
                location.reload();
                //$("#table").trigger("reloadGrid");
            });

            $('.modal-body', this).css({ width: 'auto', height: 'auto', 'max-height': '500%', position: 'center' });

            $('[data-rel=tooltip]').tooltip();

            $("form").submit(function () {
                var currentForm = this;
                bootbox.confirm(function (result) {
                    var _result = result;
                    if (result) {
                        $.ajax({
                            url: "DeleteAll",
                            type: "post",
                            dataType: "json",
                            data: $(currentForm).serialize(),
                            success: function (result) {
                                if (result.success) {
                                    location.reload();
                                } else {
                                    //$("#Error").html('Error');
                                }
                            }
                        });
                    }
                })
                return false;
            });

        })

    </script>
}

